<template>
  <list-layout :categories="categories"
    :originalCategory="Bus.$store.state.portal.filters.standard"
    @changed="categoryChanged" channel="standard">
    <template slot="bottom-of-categories">
      <div class="col q-mt-md cursor-pointer" v-if="hasPermForStdDev">
        <q-img
          src="images/portal/标准意见征集.png"
          @click="$router.push('/stddev')">
          <div class="full-height full-width text-center
            bg-transparent column justify-center text-bold">
            <div style="font-size: 1.2rem;">{{$t('标准制修订')}}</div>
            <div style="font-size: 0.875rem"
              v-if="$i18n.locale !== 'en-us'">STANDARD DEVELOPMENT</div>
          </div>
        </q-img>
      </div>

      <div class="col q-mt-md cursor-pointer">
        <q-img
          src="images/portal/标委会.png"
          @click="$router.push('/committee')">
          <div class="full-height full-width text-center
            bg-transparent column justify-center text-bold">
            <div style="font-size: 1.2rem;">{{$t('标委会')}}</div>
          </div>
        </q-img>
      </div>
    </template>
    <div class="list-top-text">
      {{$t((currentCategory && currentCategory.类型) || '全部标准')}}
      <filter-options ref="filterOptions" class="filter-options"
        :hasKw="true"
        :bottomKw="true"
        :opts="filterOptions"
        :showSelected="true"
        :maxLines="3"
        @search="filter({query: $event})"></filter-options>
    </div>
    <list-item
      v-for="(item,index) in data.docs" :key="index"
      :tags="[item.发布日 ? new Date(item.发布日).getFullYear() : undefined,
        item.类型, item.领域, item.状态]"
      :label="item.标准号"
      :title="item.名称"
      :content="item.英文名 || item.范围 || item.名称"
      :img="item.图片 && item.图片.id"
      :url="`/standard/${item.id}`"></list-item>

    <div
      v-if="data && data.total === 0"
      class="no-data-label">
      <div class="absolute-center">
        {{$t('暂无数据')}}
      </div>
    </div>

    <!-- 列表页面注意分页 -->
    <!-- 翻页时调用: refreshData({page: <页吗>}) -->
    <pagination v-if="data && data.total" :data="data" @changed="pageChanged"></pagination>
  </list-layout>
</template>

<script>
import mixins from 'eis-admin-mixins';
import { canI } from '@/utils/api.js';
import { RefreshOnLocaleChange } from '../../components/mixins/locale';
import listLayout from '../../components/listLayout';
import listItem from '../../components/listItem';
import pagination from '../../components/pagination';
import FilterOptions from '../../components/filterOptions';

export default {
  name: 'StandardList',
  mixins: [mixins.ObjectDataMixin, RefreshOnLocaleChange],
  components: {
    listItem,
    listLayout,
    pagination,
    FilterOptions,
  },
  data() {
    return {
      filters: {},
      filterOptions: [
        {
          label: '发布时间',
          field: '发布日',
          multiple: false,
          options: Array.from({ length: 21 }).map((_, opt) => {
            let from1970 = ((opt + (new Date()).getFullYear() - 20) - 1970) * 31536000000;
            from1970 -= (3600000 * 8); // 矫正时区

            return (opt === 0) ? {
              label: '更早',
              value: `0~${from1970 + 31536000000}`,
            } : {
              label: `${opt + (new Date()).getFullYear() - 20}`,
              value: `${from1970}~${from1970 + 31536000000}`,
            };
          }).reverse(),
        },
      ],
      categories: [
        {
          text: '全部',
          filter: {},
        },
        {
          text: '团体标准',
          filter: {
            类型: '团体标准',
          },
        },
      ],
      currentCategory: '',
      hasPermForStdDev: false,
    };
  },
  created() {
    if (this.Bus && this.Bus.$store && this.Bus.$store.state
      && this.Bus.$store.state.portal && this.Bus.$store.state.portal.filters
      && this.Bus.$store.state.portal.filters.standard) {
      this.currentCategory = this.Bus.$store.state.portal.filters.standard.filter || {};

      this.refreshData({ ...this.currentCategory });
    } else {
      this.refreshData();
    }

    canI('/api/stddev/dev', true).then((r) => {
      if (r) {
        this.hasPermForStdDev = true;
      }
    });
  },
  methods: {
    afterRefresh() {
      this.Bus.$store.commit('portal/CLEAR_FILTERS');
    },
    categoryChanged(f) {
      this.currentCategory = f;

      this.refreshData(f);

      this.filters = {};
      this.$refs.filterOptions.clearFilters();
    },
    pageChanged(p) {
      let filter = {};

      if (this.currentCategory) {
        filter = { ...this.currentCategory };
      }

      this.refreshData({ ...this.filters, ...filter, page: p });
    },
    filter(filters) {
      const { query } = filters;

      this.filters = query || {};
      let filter = {};

      if (this.currentCategory) {
        filter = { ...this.currentCategory };
      }

      this.refreshData({ ...this.filters, ...filter });
    },
  },
};
</script>

<style lang="scss" scoped>
.filter-options {
  font-size: 14px;
}
</style>
